/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.nicefotoappcamp.provider.fotokudra;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.me.nicefotoappcamp.core.Picture;
import org.me.nicefotoappcamp.provider.Filter;
import org.me.nicefotoappcamp.provider.HttpProvider;


/**
 *
 * @author Tadas
 */
public class FotoKudraProvider extends HttpProvider {

    final private static String picturesLink = "http://fotokudra.lt/top_week.php?mnx=1";

    public Picture getRandomPicture() {
        return null;
    }

    public void comment(Picture pic, String msg) {
    }

    public void rate(Picture pic, int rate) {
    }

    public ArrayList<Picture> getPictures() {
        ArrayList<Picture> pictures = new ArrayList<Picture>();
        String html = request(picturesLink);
        ArrayList<String> smallPhotoLinks = getSmallPhotoLinks(html);
        for(String part : smallPhotoLinks){
            String url = "http://fotokudra.lt/files/" + part;
            byte[] file = getFile(url);
            pictures.add(new Picture((file), 0, 0, null));
        }
        return pictures;
    }

    public ArrayList<String> getSmallPhotoLinks(){
        String html = request(picturesLink);
        return getSmallPhotoLinks(html);
    }

    public ArrayList<Picture> getPictures(Filter filter) {
        return null;
    }

    public void favorite(Picture pic) {
    }

    protected ArrayList<String> getSmallPhotoLinks(String html) {
        ArrayList<String> found = new ArrayList<String>();
      
        Pattern p = Pattern.compile("<img  class=\"borders_white\" style=\"border: solid 0px #fff;\"  src=\"files//(S[0-9]+\\.jpg)\" ></a><br>");
       
        Matcher m = p.matcher(html);
        while (m.find()) {
            System.out.println(m.group(1));
            found.add(m.group(1));
        }
        return found;
    }
}
